import time
import traceback
from .config import Config
from .logger import Logger
from .reporter import Reporter

def runtime(func, *args, **kwargs):
    start = time.time()
    try:
        config = Config()
        result = func(*args, **kwargs)
        host = config.MAIL.HOST
        user = config.MAIL.USER
        password = config.MAIL.PASSWORD
        from_email = config.MAIL.FROM_EMAIL
        to_email = config.MAIL.TO_EMAIL
    except Exception as e:
        Logger.error(traceback.format_exc())
        message = "程序运行出错。"
    finally:
        message = f"程序运行成功，运行时间为{time.time() - start:.4f}秒。"
    Reporter(host, user, password).send_mail("程序运行结果", message, from_email, to_email)
    return result